home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / edm0407s.zip / STAT3SRC.ZIP / STATLINE.CPP < prev    next >
C/C++ Source or Header  |  1996-07-25  |  22KB  |  477 lines

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // STATLINE.CPP - description of AStatusLineSample, sample-program for the progress-indicating status line
  4. //
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #define INCL_WINERRORS
  10. #define INCL_GPIERRORS
  11. #define INCL_DOSPROCESS
  12. #define INCL_DOSSEMAPHORES
  13. #define INCL_DOSERRORS
  14.   
  15. #include <os2.h>
  16. #include <imsgbox.hpp>
  17. #include <ifiledlg.hpp>
  18. #include <icolor.hpp>
  19.  
  20. #include "statline.hpp"
  21. #include "statline.rch"
  22. #include "about.hpp"
  23.  
  24. //*************************************************************************
  25. // main  - Application entry point                                        *
  26. //*************************************************************************
  27. int main(int argc, char ** argv)
  28. {
  29.   AStatusLineSample mainWindow (WND_MAIN);        // Create our main window
  30.  
  31.   IApplication :: current (). run ();             // Get the current application and run it
  32.  
  33.   return (0);
  34. }
  35.  
  36. //********************************************************************************
  37. // AStatusLineSample :: AStatusLineSample - Constructor of AStatusLineSample     *
  38. //********************************************************************************
  39. AStatusLineSample :: AStatusLineSample(unsigned long windowId)
  40.                : IFrameWindow (IFrameWindow::titleBar
  41.                                | IFrameWindow::systemMenu
  42.                                | IFrameWindow::sizingBorder
  43.                                | IFrameWindow::minimizeButton
  44.                                | IFrameWindow::maximizeButton
  45.                                | IFrameWindow::windowList
  46.                                | IFrameWindow::appDBCSStatus
  47.                                | IFrameWindow::minimizedIcon
  48.                                | IFrameWindow::menuBar
  49.                                | IFrameWindow::accelerator,
  50.                                windowId)
  51. {
  52.   setupVariables ();                                // setup the variables
  53.  
  54.   setupStatusLine ();                             // setup the status line
  55.   
  56.   IKeyboardHandler :: handleEventsFor (this);     // set self as keyboard handler
  57.  
  58.   ICommandHandler :: handleEventsFor (this);      // set self as command handler
  59.  
  60.   AUserMessageHandler :: handleEventsFor (this);  // set self as user message handler
  61.  
  62.   setFocus ();                                    // set the focus
  63.  
  64.   show ();                                        // show the window
  65. }
  66.  
  67. //********************************************************************************
  68. // AStatusLineSample :: ~AStatusLineSample - Destructor of AStatusLineSample     *
  69. //********************************************************************************
  70. AStatusLineSample :: ~AStatusLineSample(void)
  71.  
  72. {
  73.   if(m_pMenuBar)                                          // delete the menu bar
  74.      delete m_pMenuBar;
  75.  
  76.   if(m_pFileThread)                                       // is a file thread active?
  77.      {                                                    // yes
  78.       if(m_pEditFile)                                     // does a file exists?
  79.          m_pEditFile -> stopProcess ();                   // yes: stop the process
  80.       else
  81.          m_pFileThread -> stop ();                        // no: kill the thread
  82.       IThread :: current(). waitFor (*m_pFileThread);     // wait till everything is done
  83.       delete m_pFileThread;                               // delete the object
  84.       m_pFileThread = 0L;                                 // reset the pointer
  85.      }
  86.  
  87.   if(m_pEditFile)                                         // delete the file
  88.      delete m_pEditFile;
  89.  
  90.   if(m_pText)                                             // delete the text
  91.      delete m_pText;
  92. }
  93.  
  94. //********************************************************************************
  95. // AStatusLineSample :: setupVariables - variable set up                         *
  96. //********************************************************************************
  97. void AStatusLineSample :: setupVariables (void)
  98. {
  99.   m_pFileThread = 0L;
  100.  
  101.   m_pEditFile = 0L;
  102.  
  103.   m_pMenuBar = new IMenuBar (WND_MAIN, this);
  104.  
  105.   m_pText = new IStaticText (WND_TEXT, this, this);
  106.  
  107.   m_pText -> setColor (IStaticText :: foreground, IColor :: red);  // set the foreground color of the text to red
  108.  
  109.   m_pText -> setText (STR_TEXT);                                   // set the text displayed in the client area
  110.  
  111.   m_pText -> setAlignment (IStaticText :: centerCenter);           // set the position of the text
  112.  
  113.   setClient (m_pText);                                             // set the text as client of the framewindow
  114. }
  115.  
  116. //********************************************************************************
  117. // AStatusLineSample :: setupStatusLine - setting up the StatusLine              *
  118. //********************************************************************************
  119. void AStatusLineSample :: setupStatusLine (void)
  120. {
  121.   IPresSpaceHandle hps = m_pMenuBar -> presSpace ();          // get the presspace from the menu bar
  122.  
  123.   IFont * pMenueFont = new IFont (hps);                       // get the font of the menu bar
  124.  
  125.   releasePresSpace (hps);                                     // release the presspace
  126.  
  127.   m_pStatusLine = new AProcessStatus (this);                  // create the status line
  128.  
  129.   addExtension (m_pStatusLine, IFrameWindow :: belowClient,   // set the size and position of the statusline
  130.                 pMenueFont -> maxAscender() + (ULONG) 2 * pMenueFont -> maxDescender ());
  131.  
  132.   m_pStatusLine -> setFont (*pMenueFont);                     // set the font of the status line to the font of the menu bar
  133.  
  134.   delete pMenuFont;                                           // delete the font
  135. }
  136.  
  137. //********************************************************************************
  138. // AStatusLineSample :: virtualKeyPress- virtual-key-event processing            *
  139. //********************************************************************************
  140. Boolean AStatusLineSample :: virtualKeyPress (IKeyboardEvent& event)
  141.  
  142. {
  143.   if(event. virtualKey () == IKeyboardEvent :: esc)  // did the user pressed the esc-key?
  144.      {                                               // yes
  145.       if(m_pFileThread                               // is there is a reading/writing thread running=
  146.          && m_pEditFile)                             // yes
  147.          m_pEditFile -> stopProcess ();              // stop it
  148.       return (true);                                 // event handled
  149.      }
  150.  
  151.   return (false);                                    // event not handled
  152. }
  153.  
  154. //********************************************************************************
  155. // AStatusLineSample :: command - handles Command-Events                         *
  156. //********************************************************************************
  157. Boolean AStatusLineSample :: command(ICommandEvent &evt)
  158. {
  159.   Boolean filtered = true;                                        // event handled
  160.   ULONG ulCommandId = evt.commandId();
  161.  
  162.   switch(ulCommandId)                                               // handle the menu commands
  163.          {
  164.           case MI_FILE_OPEN:                                      // open a file
  165.                startOpenFile ();
  166.                break;
  167.           case MI_FILE_SAVE:                                      // save the file
  168.                startSaveFile ();
  169.                break;
  170.           case MI_FILE_EXIT:                                      // exit the program
  171.                if(m_pFileThread)                                    // is a file thread active?
  172.                   {                                               // yes
  173.                    if(m_pEditFile)                                // does a file object exists
  174.                       m_pEditFile -> stopProcess ();              // yes: stop the processing
  175.                    IThread :: current(). waitFor (*m_pFileThread);// wait till the thread has finished
  176.                    delete m_pFileThread;                          // delete the thread object
  177.                    m_pFileThread = 0L;                            // reset the pointer
  178.                   }
  179.                close ();                                          // close the framewindow
  180.                break;
  181.           case MI_HELP_ABOUT:                                     // show the product information
  182.                {
  183.                 AAbout * pDlgAbout = new AAbout (this);           // create the dialog box
  184.                 pDlgAbout -> showModally ();                      // show it
  185.                 delete pDlgAbout;                                 // delete it
  186.                 break;
  187.                }
  188.           default:
  189.               filtered = false;                                   // event not handled
  190.               break;
  191.          }
  192.  
  193.   return filtered;
  194. }
  195.  
  196. //********************************************************************************
  197. // AStatusLineSample :: startOpenFile - starts opening a file, starts thread     *
  198. //********************************************************************************
  199. Boolean AStatusLineSample :: startOpenFile (void)
  200.  
  201. {
  202.   if(m_pFileThread)                                                         // is a file thread active?
  203.      {
  204.       IMessageBox msgBox (this);                                            // yes
  205.       if(!loadString (STR_FILE_OPEN))                                       // load the message title
  206.          m_String = "Open a file";
  207.       msgBox.setTitle (m_String);                                           // set the title
  208.       if(!loadString (STR_ALREADY_PROCEEDING))                              // load the message
  209.          m_String = "A file process is still in progress.";
  210.       msgBox.show (m_String, IMessageBox::okButton   |                      // show it
  211.                              IMessageBox::errorIcon  |
  212.                              IMessageBox::applicationModal |
  213.                              IMessageBox::moveable);
  214.       return (false);                                                          // abort
  215.      }
  216.  
  217.   if(!loadString (STR_FILE_OPEN))                                           // load the title for the open file dialog
  218.      m_String = "Open a file";
  219.  
  220.   IFileDialog :: Settings fsettings;    
  221.  
  222.   fsettings. setTitle (m_String);                                           // set the title
  223.  
  224.   fsettings. setFileName ("*.*");                                           // set the filename
  225.  
  226.   IFileDialog fd( (IWindow*) desktopWindow (), (IWindow*) this, fsettings); // show the open file dialog
  227.  
  228.   if (!fd. pressedOK ())                                                    // if not pressed "OK", abort
  229.       return (false);
  230.  
  231.   m_sFilename = fd.fileName ();                                             // save the filename
  232.  
  233.   m_pFileThread = new IThread ();                                           // create a thread object
  234.  
  235.   m_pFileThread -> start(new IThreadMemberFn <AStatusLineSample> (*this, AStatusLineSample :: openFile));   // start the read-thread
  236.  
  237.   return (true);                                                            // return
  238. }
  239.  
  240. //********************************************************************************
  241. // AStatusLineSample :: openFile - thread-function to open a file                *
  242. //********************************************************************************
  243. void AStatusLineSample :: openFile (void)
  244.  
  245. {
  246.   APIRET ret;
  247.  
  248.   DosSetPriority (PRTYS_THREAD, PRTYC_REGULAR, 0L, 0UL);            // set the priority of the current thread
  249.  
  250.   Boolean bError = false;                                           // reset the error-flag
  251.  
  252.   if(m_pEditFile)                                                   // delete an existing file object
  253.      {
  254.       delete m_pEditFile;
  255.       m_pEditFile = 0L;
  256.      }
  257.  
  258.   if(!(m_pEditFile = new AEditFile ()))                             // create a new file object
  259.      goto endOpenFile;
  260.  
  261.   if(m_pEditFile -> open (m_sFilename, 0L, FILE_NORMAL, FILE_OPEN)) // try to open the file
  262.      {
  263.       bError = true;
  264.       goto endOpenFile;
  265.      }
  266.  
  267.   try
  268.      {
  269.       m_pEditFile -> allocateDataBuffer ();                         // try to allocate the data buffer
  270.      }
  271.  
  272.   catch (ULONG ulSize)                                              // can't get the file size or it is 0
  273.          {
  274.           bError = true;
  275.           goto endOpenFile;
  276.          }
  277.  
  278.   catch (char * pszBuffer)                                          // can't allocate the buffer
  279.          {
  280.           bError = true;
  281.           goto endOpenFile;
  282.          }
  283.  
  284.   catch (...)                                                       // some other exceptions
  285.          {
  286.           Boolean bPMAlreadyInit = true;
  287.           ICurrentThread& rCurrThread = IThread :: current();       // get the current thread
  288.           if (rCurrThread. isPMInitialized() != true)               // is the pm environment active for this thread?
  289.               {                                                     // no
  290.                bPMAlreadyInit = false;                              // set the flag
  291.                rCurrThread. initializePM ();                        // initialize it
  292.               }
  293.           bError = true;                                            // set the error flag
  294.           IMessageBox msgBox (this);                                // create and show a message box
  295.           msgBox.setTitle ("Unexpected Error");
  296.           msgBox.show ("Unexpected Error in AStatusLineSample :: openFile; AEditFile :: allocateDataBuffer", 
  297.                        IMessageBox::okButton   |
  298.                        IMessageBox::errorIcon  |
  299.                        IMessageBox::applicationModal |
  300.                        IMessageBox::moveable);
  301.           if(!bPMAlreadyInit)                                       // was pm active for this thread?
  302.              rCurrThread. terminatePM();                            // no: terminate it
  303.           goto endOpenFile;                                         // quit
  304.          }
  305.  
  306.   m_startParams. ulProcessSize = m_pEditFile -> getFileSize();      // set the start parameters for the status line
  307.   m_startParams. ulTickSize = AEditFile :: eBytesToRead;
  308.   m_startParams. lResourceId = STR_FILE_READ;
  309.  
  310.   WinPostMsg (m_pStatusLine -> handle(), MYM_STATUS_START_PROCESS, MPFROMP (&m_startParams), 0); // post them
  311.  
  312.   ret = m_pEditFile -> read (m_pStatusLine -> handle ());           // read the file
  313.  
  314.   if(ret)                                                           // was it successful?
  315.      {                                                              // no
  316.       bError = true;                                                // set the flag
  317.       goto endOpenFile;                                             // quit
  318.      }
  319.  
  320.   if(m_pEditFile -> close())                                        // try to close the file
  321.      {
  322.       bError = true;
  323.       goto endOpenFile;
  324.      }
  325.  
  326.   endOpenFile:
  327.  
  328.   if(bError)                                                        // did an error occured?
  329.      {                                                              // yes
  330.       delete m_pEditFile;                                           // delete the file
  331.       m_pEditFile = 0L;                                             // reset the pointer
  332.      }
  333.  
  334.   WinPostMsg (m_pStatusLine -> handle(), MYM_STATUS_END_PROCESS, 0, 0); // let the status line know the process has finished
  335.  
  336.   WinPostMsg (handle(), MYM_OPENFILE_READY, 0, 0);                  // let the frame window know the process has finished
  337. }
  338.  
  339. //********************************************************************************
  340. // AStatusLineSample :: openFileReady - perhabs the wanted file is open          *
  341. //********************************************************************************
  342. Boolean AStatusLineSample :: openFileReady (IEvent& evt)
  343.  
  344. {
  345.   if(!m_pFileThread)                                // does a sub-thread exists?
  346.      return (true);                                 // no: return
  347.  
  348.   IThread :: current(). waitFor (*m_pFileThread);   // yes: wait till it is ready
  349.  
  350.   delete m_pFileThread;                             // delete the object
  351.  
  352.   m_pFileThread = 0L;                               // reset the pointer
  353.  
  354.   return (true);
  355. }
  356.  
  357. //********************************************************************************
  358. // AStatusLineSample :: startSaveFile - starts saving a file, starts thread      *
  359. //********************************************************************************
  360. Boolean AStatusLineSample :: startSaveFile (void)
  361.  
  362. {
  363.   if(m_pFileThread)                                                         // is a file thread active?
  364.      {
  365.       IMessageBox msgBox (this);                                            // yes
  366.       if(!loadString (STR_FILE_SAVE))                                       // load the message title
  367.          m_String = "Save a file";
  368.       msgBox.setTitle (m_String);                                           // set the title
  369.       if(!loadString (STR_ALREADY_PROCEEDING))                              // load the message
  370.          m_String = "A file process is still in progress.";
  371.       msgBox.show (m_String, IMessageBox::okButton   |                      // show it
  372.                              IMessageBox::errorIcon  |
  373.                              IMessageBox::applicationModal |
  374.                              IMessageBox::moveable);
  375.       return (false);                                                          // abort
  376.      }
  377.  
  378.   m_pFileThread = new IThread ();                                           // create a thread object
  379.  
  380.   m_pFileThread -> start(new IThreadMemberFn <AStatusLineSample> (*this, AStatusLineSample :: saveFile));   // start the write-thread
  381.  
  382.   return (true);                                                            // return
  383. }
  384.  
  385. //********************************************************************************
  386. // AStatusLineSample :: saveFile - thread-function to save a file                *
  387. //********************************************************************************
  388. void AStatusLineSample :: saveFile (void)
  389.  
  390. {
  391.   APIRET ret;
  392.  
  393.   DosSetPriority( PRTYS_THREAD, PRTYC_REGULAR, 0L, 0UL );           // set the priority of the current thread
  394.  
  395.   Boolean bError = false;                                           // reset the error-flag
  396.  
  397.   if(!m_pEditFile)                                                  // is there a file to save?
  398.      {                                                              // no
  399.       goto endSaveFile;                                             // quit
  400.      }
  401.  
  402.   if(m_pEditFile -> reOpen (m_pEditFile -> getFileSize (), FILE_NORMAL, FILE_OPEN)) // try to re-open the file
  403.      {
  404.       bError = true;
  405.       goto endSaveFile;
  406.      }
  407.  
  408.   m_startParams. ulProcessSize = m_pEditFile -> getFileSize();      // set the start parameters for the status line
  409.   m_startParams. ulTickSize = AEditFile :: eBytesToRead;
  410.   m_startParams .lResourceId = STR_FILE_WRITE;
  411.  
  412.   WinPostMsg (m_pStatusLine -> handle(), MYM_STATUS_START_PROCESS, MPFROMP (&m_startParams), 0); // post them
  413.  
  414.   ret = m_pEditFile -> write (m_pStatusLine -> handle ());          // write the file
  415.  
  416.   if(ret)                                                           // was it successful?
  417.      {                                                              // no
  418.       bError = true;                                                // set the flag
  419.       goto endSaveFile;                                             // quit
  420.      }
  421.  
  422.   if(m_pEditFile -> close())                                        // try to close the file
  423.      {
  424.       bError = true;
  425.       goto endSaveFile;
  426.      }
  427.  
  428.   endSaveFile:
  429.  
  430.   if(bError)                                                        // did an error occured?
  431.      {                                                              // yes
  432.       delete m_pEditFile;                                           // delete the file
  433.       m_pEditFile = 0L;                                             // reset the pointer
  434.      }
  435.  
  436.   WinPostMsg (m_pStatusLine -> handle(), MYM_STATUS_END_PROCESS, 0, 0); // let the status line know the process has finished
  437.  
  438.   WinPostMsg (handle(), MYM_SAVEFILE_READY, 0, 0);                  // let the frame window know the process has finished
  439. }
  440.  
  441. //********************************************************************************
  442. // AStatusLineSample :: saveFileReady - perhabs the wanted file is written       *
  443. //********************************************************************************
  444. Boolean AStatusLineSample :: saveFileReady (IEvent& evt)
  445.  
  446. {
  447.   if(!m_pFileThread)                                // does a sub-thread exists?
  448.      return (true);                                 // no: return
  449.  
  450.   IThread :: current(). waitFor (*m_pFileThread);   // yes: wait till it is ready
  451.  
  452.   delete m_pFileThread;                             // delete the object
  453.  
  454.   m_pFileThread = 0L;                               // reset the pointer
  455.  
  456.   return (true);
  457. }
  458.  
  459. //********************************************************************************
  460. // AStatusLineSample :: loadString - loads a string from the resource library    *
  461. //********************************************************************************
  462. Boolean AStatusLineSample :: loadString (unsigned long ulStringId)
  463.  
  464. {
  465.   try
  466.      {
  467.       m_String = IApplication :: current (). userResourceLibrary (). loadString (ulStringId);  // try to load the string
  468.      }
  469.  
  470.   catch (IException&)       // if not successful
  471.          {
  472.           return (false);   // return false
  473.          }
  474.   
  475.   return (true);            // string loaded
  476. }
  477.